#!/bin/bash

# 设置颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 设置变量
PROJECT_ROOT="NBA_game"
SCRIPT_DIR="nbaGameAnalysis/PythonScript"
DATA_DIR="nbaGameAnalysis/data"
SCRIPT_NAME="calculate_avg_points_pyspark.py"
DATA_FILE="game_processed.csv"

echo -e "${GREEN}开始部署NBA数据分析脚本...${NC}"

# 1. 创建目录结构
echo "创建目录结构..."
mkdir -p ~/${PROJECT_ROOT}/${SCRIPT_DIR}
mkdir -p ~/${PROJECT_ROOT}/${DATA_DIR}

# 2. 复制文件到对应目录
echo "复制文件到目标目录..."
cp ${SCRIPT_NAME} ~/${PROJECT_ROOT}/${SCRIPT_DIR}/
cp ../${DATA_DIR}/${DATA_FILE} ~/${PROJECT_ROOT}/${DATA_DIR}/

# 3. 设置执行权限
echo "设置执行权限..."
chmod +x ~/${PROJECT_ROOT}/${SCRIPT_DIR}/${SCRIPT_NAME}

# 4. 切换到脚本目录
cd ~/${PROJECT_ROOT}/${SCRIPT_DIR}

# 5. 运行Spark作业
echo -e "${GREEN}开始运行Spark分析作业...${NC}"
if spark-submit ${SCRIPT_NAME}; then
    echo -e "${GREEN}分析完成！${NC}"
    echo "结果文件保存在: ~/${PROJECT_ROOT}/${DATA_DIR}/season_avg_points_spark.csv"
    
    # 显示结果文件的前几行
    echo -e "\n${GREEN}结果预览：${NC}"
    head -n 5 ../data/season_avg_points_spark.csv
else
    echo -e "${RED}运行失败，请检查错误信息${NC}"
    exit 1
fi
